package com.sec.android.app.myfiles.launch;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.text.TextUtils;

import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.CategoryType;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.module.local.category.CategoryFileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo.NavigationMode;
import com.sec.android.app.myfiles.util.FileUtils;

public class HandlePickerImp extends AbsHandleLaunchImp {


    private String[] mMimeTypeList;
    private String[] mExtensionList;
    private CategoryType mCategoryFilter;

    private static final String CTS_CERTIFICATE = "/myCA.cer";
    private static final String CTS_CERTIFICATE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + CTS_CERTIFICATE;

    @Override
    public boolean isMyJob(Intent intent) {
        boolean bRet = false;

        final String action = intent.getAction();
        if (AppConstants.Action.PICK_SINGLE.equals(action) || AppConstants.Action.PICK_MULTIPLE.equals(action)) {
            bRet = true;
        }

        return bRet;
    }

    @Override
    public boolean _handleStart(int processId, Activity activity, Intent intent) {
        NavigationMode mode = getPickerMode(intent);
        FileRecord homePath = getHomeRecord(intent, mode);
        checkNeedScan(intent, activity.getApplicationContext());
        int maxCount = intent.getIntExtra(AppConstants.MyFilesExtras.EXTRA_FILE_SELECTION_MAX_COUNT, AppConstants.SELECT_LIMIT_COUNT);
        String storagePath = getStartPath(intent);
        String prefix = intent.getStringExtra(AppConstants.MyFilesExtras.EXTRA_PREFIX_FOR_EXTENSION);

        MyFilesFacade.launchPicker(processId, homePath, mode, mMimeTypeList, mExtensionList, maxCount, storagePath, mCategoryFilter, prefix);

        return true;
    }

    private void checkNeedScan(Intent intent, Context context) {
        if (intent.getBooleanExtra(AppConstants.MyFilesExtras.EXTRA_NEED_MEDIA_SCAN, false)) {
            FileUtils.sendScanSingleFile(context, CTS_CERTIFICATE_PATH);
        }
    }

    private FileRecord getHomeRecord(Intent intent, NavigationMode mode) {
        FileRecord ret;

        CategoryType category = getCategory(intent);
        mCategoryFilter = getCategoryFilter(category);
        mMimeTypeList = getMimeType(intent, category);
        mExtensionList = getExtensions(intent);

        if (category != CategoryType.None || mMimeTypeList != null || mExtensionList != null) {
            ret = FileRecord.createFileRecord(StorageType.Category, CategoryFileRecord.getCategoryPath(category));
        } else {
            String path = intent.getStringExtra(AppConstants.MyFilesExtras.EXTRA_FOLDER_PATH);

            if (path == null) {
                path = intent.getStringExtra(AppConstants.MyFilesExtras.EXTRA_START_FOLDER);
                if (path == null && AppFeatures.isTabletUIMode() && mode != NavigationMode.Pick_one_file) {
                    path = AppConstants.StoragePath.INTERNAL_ROOT;
                }
            }

            if (path != null) {
                ret = FileRecord.createFileRecord(StorageType.Local, path);
            } else {
                ret = FileRecord.createFileRecord(StorageType.Home, "");
            }
        }

        return ret;
    }

    private NavigationMode getPickerMode(Intent intent) {
        NavigationMode ret = NavigationMode.Pick_files;

        String action = intent.getAction();
        if (AppConstants.Action.PICK_SINGLE.equals(action)) {
            ret = NavigationMode.Pick_one_file;
        }

        return ret;
    }

    private String getStartPath(Intent intent) {
        String path = intent.getStringExtra(AppConstants.MyFilesExtras.EXTRA_FOLDER_PATH);
        return path;
    }

    private String[] getMimeType(Intent intent, CategoryType categoryType) {
        String[] ret = null;

        String mimeType = intent.getStringExtra(AppConstants.MyFilesExtras.EXTRA_CONTENT_TYPE);
        if (mimeType != null) {
            if (CategoryType.Image.equals(categoryType)) {
                mimeType = mimeType.replace("image/*", "");
            }
            if (CategoryType.Audio.equals(categoryType)) {
                mimeType = mimeType.replace("audio/*", "");
            }
            if (CategoryType.Video.equals(categoryType)) {
                mimeType = mimeType.replace("video/*", "");
            }

            if (!TextUtils.isEmpty(mimeType))
                ret = mimeType.split(";");
        }

        if (ret != null) {
            for (String value : ret) {
                if (value.startsWith("*")) {
                    ret = null;
                    break;
                }
            }
        }
        Log.d(this, "getMimeType - " + mimeType);
        return ret;
    }

    private String[] getExtensions(Intent intent) {
        String[] ret = null;

        String extensions = intent.getStringExtra(AppConstants.MyFilesExtras.EXTRA_CONTENT_EXTENSION);
        if (extensions != null) {
            ret = extensions.split(";");
        }

        Log.d(this, "getExtensions - " + extensions);
        return ret;
    }

    private CategoryType getCategoryFilter(CategoryType category) {
        if (category == CategoryType.None) {
            return null;
        } else {
            return category;
        }
    }
}
